#include "config_manager.h"
#include <QDebug>
#include <QDir>
#include <QCoreApplication>
#include <QFileInfo>
#include <QDateTime>

ConfigManager::ConfigManager(QObject *parent)
    : QObject(parent)
    , m_settings(nullptr)
    , m_saveToFile(true)
    , m_outputFilePath("")
{
    // 设置配置文件路径
    QString appDir = QCoreApplication::applicationDirPath();
    m_configFilePath = appDir + "/audio_capture_config.ini";
    
    // 加载配置
    loadConfiguration();
}

bool ConfigManager::getSaveToFile() const
{
    return m_saveToFile;
}

QString ConfigManager::getOutputFilePath() const
{
    return m_outputFilePath;
}

QString ConfigManager::getConfigFilePath() const
{
    return m_configFilePath;
}

void ConfigManager::setSaveToFile(bool saveToFile)
{
    m_saveToFile = saveToFile;
    qDebug() << "配置已更新: SaveToFile =" << m_saveToFile;
}

void ConfigManager::setOutputFilePath(const QString &filePath)
{
    if (isValidOutputPath(filePath)) {
        m_outputFilePath = filePath;
        qDebug() << "配置已更新: OutputFilePath =" << m_outputFilePath;
    } else {
        qWarning() << "无效的输出文件路径:" << filePath;
    }
}

void ConfigManager::loadConfiguration()
{
    m_settings = new QSettings(m_configFilePath, QSettings::IniFormat);
    
    // 读取配置，如果没有则使用默认值
    m_saveToFile = m_settings->value("AudioCapture/SaveToFile", true).toBool();
    m_outputFilePath = m_settings->value("AudioCapture/OutputFilePath", getDefaultOutputPath()).toString();
    
    qDebug() << "已加载配置文件:" << m_configFilePath;
    qDebug() << "  保存到文件:" << m_saveToFile;
    qDebug() << "  输出文件路径:" << m_outputFilePath;
}

void ConfigManager::saveConfiguration()
{
    if (m_settings) {
        m_settings->setValue("AudioCapture/SaveToFile", m_saveToFile);
        m_settings->setValue("AudioCapture/OutputFilePath", m_outputFilePath);
        m_settings->sync();
        qDebug() << "配置已保存到:" << m_configFilePath;
    }
}

void ConfigManager::resetToDefaults()
{
    m_saveToFile = true;
    m_outputFilePath = getDefaultOutputPath();
    
    qDebug() << "配置已重置为默认值";
    qDebug() << "  保存到文件:" << m_saveToFile;
    qDebug() << "  输出文件路径:" << m_outputFilePath;
    
    // 保存到文件
    saveConfiguration();
}

void ConfigManager::showCurrentConfig() const
{
    qDebug() << "=== 当前配置 ===";
    qDebug() << "配置文件:" << m_configFilePath;
    qDebug() << "保存到文件:" << (m_saveToFile ? "是" : "否");
    qDebug() << "输出文件路径:" << m_outputFilePath;
    qDebug() << "=================";
}

QString ConfigManager::getDefaultOutputPath() const
{
    QString appDir = QCoreApplication::applicationDirPath();
    QString timestamp = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss");
    return appDir + "/captured_audio_" + timestamp + ".wav";
}

bool ConfigManager::isValidOutputPath(const QString &path) const
{
    if (path.isEmpty()) {
        return true; // 空路径表示使用默认路径
    }
    
    QFileInfo fileInfo(path);
    QString dirPath = fileInfo.absolutePath();
    
    // 检查目录是否存在或可创建
    QDir dir(dirPath);
    if (!dir.exists()) {
        // 尝试创建目录
        if (!dir.mkpath(".")) {
            return false;
        }
    }
    
    // 检查目录是否可写
    QFile testFile(dirPath + "/test_write.tmp");
    if (testFile.open(QIODevice::WriteOnly)) {
        testFile.remove();
        return true;
    }
    
    return false;
}

QString ConfigManager::getFormattedConfigInfo() const
{
    QString info;
    info += "=== 音频捕获配置 ===\n";
    info += QString("配置文件: %1\n").arg(m_configFilePath);
    info += QString("保存到文件: %1\n").arg(m_saveToFile ? "是" : "否");
    info += QString("输出文件路径: %1\n").arg(m_outputFilePath.isEmpty() ? "默认路径" : m_outputFilePath);
    info += "===================";
    return info;
}
